% Read the Jic_delh 150 Km echo data, combine them

% June 17, 2011



S = dir('E:\projects\ace_tensor\piu_jic_deltah\jro.igp.gob.pe\database\drifts\data\mag-dh\*.txt');

localdirectory = 'E:\projects\ace_tensor\piu_jic_deltah\jro.igp.gob.pe\database\drifts\data\mag-dh\';

Jic_delh_W = zeros([132,length(S)])'*NaN;
Jic_delh_fday = Jic_delh_W;

for i = 1:length(S),
        
    [data] = textread([ localdirectory S(i).name] ,'%f','delimiter',',','emptyvalue',NaN,'headerlines',6);
    
    data = reshape(data',[5,length(data)/5])';
        
    year = str2double(S(i).name(9:12));
    dayoftheyear = str2double(S(i).name(13:15));
    
    fday = datenum(year,1,0,data(:,1)+5,data(:,2),30) + dayoftheyear;
    %Hard writing 30 sec to tolerate small changes in time
    Jic_delh_W(i,1:length(fday)) = data(:,4);
    Jic_delh_fday(i,1:length(fday)) = fday;
    
    fprintf('%s %d\n',S(i).name,length(data));
    
    
end;

% Arrange the data according to the correct time stamps

Jic_delh_W_n = zeros([132,length(S)])'*NaN;
Jic_delh_fday_n = Jic_delh_W_n;

ncount = 0;
jfday = Jic_delh_fday(1,:)-floor(Jic_delh_fday(1,:));
for i = 1:length(S),
    
    thisfday = Jic_delh_fday(i,:)-floor(Jic_delh_fday(i,:));
    [c,ia,ib] = intersect(jfday,thisfday);
    if ~isempty(c),
        Jic_delh_W_n(i,ia) = Jic_delh_W(i,ib);
        Jic_delh_fday_n(i,ia) = Jic_delh_fday(i,ib);
    else
        ncount = ncount + 1;
    end;
end;
    
save E:\projects\ace_tensor\piu_jic_deltah\Jic_drift_2001_2011 Jic_delh_W_n Jic_delh_fday_n;

    
% for i = 1:800,
% plot(Jic_delh_fday_n(ia(i),:),Jic_delh_W_n(ia(i),:)); hold on;
% plot(Julia_fday_n(ib(i),:),Julia_W_n(ib(i),:),'r'); hold off;pause;
% end;
    
    

